import apiClient from './index';

// 获取小组排名参数
export interface GroupRankingParams {
  class_id: number;
}

// 小组排名项类型
export interface GroupRankingItem {
  group_id: number;
  group_name: string;
  leader_info: {
    leader_name: string;
    leader_student_id: number;
    leader_user_id: number;
  };
  rank: number;
  total_points: number;
}

// 小组排名响应
export interface GroupRankingResponse {
  code: number;
  content: GroupRankingItem[];
  msg: string;
}

// 创建小组参数
export interface CreateGroupParams {
  name: string;
  class_id: number;
}

// 创建小组响应
export interface CreateGroupResponse {
  code: number;
  content: {
    group_id: number;
    name: string;
  };
  msg: string;
}

// 更新小组参数
export interface UpdateGroupParams {
  group_id: number;
  name: string;
  class_id: number;
}

// 更新小组响应
export interface UpdateGroupResponse {
  code: number;
  msg: string;
}

// 获取小组成员参数
export interface GroupMembersParams {
  group_id: number;
}

// 小组成员项
export interface GroupMemberItem {
  student_id: number;
  student_number: string;
  name: string;
  is_leader: boolean;
}

// 小组成员响应
export interface GroupMembersResponse {
  code: number;
  content: GroupMemberItem[];
  msg: string;
}

// 移除小组成员参数
export interface RemoveGroupStudentParams {
  group_id: number;
  student_id: number;
}

// 设为组长参数
export interface SetGroupLeaderParams {
  group_id: number;
  leader_id: number;
}

// 添加小组成员参数
export interface AddGroupStudentsParams {
  group_id: number;
  student_ids: number[];
}

// 通用操作响应
export interface GroupOperateResponse {
  code: number;
  msg: string;
}

/**
 * 获取小组积分排名
 */
export const getGroupRanking = async (
  params: GroupRankingParams
): Promise<GroupRankingResponse> => {
  const response = await apiClient.post<GroupRankingResponse>('/point/group-ranking', params);
  return response.data;
};

/**
 * 创建小组
 */
export const createGroup = async (
  params: CreateGroupParams
): Promise<CreateGroupResponse> => {
  const response = await apiClient.post<CreateGroupResponse>('/group/create', params);
  return response.data;
};

/**
 * 更新小组信息
 */
export const updateGroup = async (
  params: UpdateGroupParams
): Promise<UpdateGroupResponse> => {
  const response = await apiClient.post<UpdateGroupResponse>('/group/update', params);
  return response.data;
};

/**
 * 获取小组成员
 */
export const getGroupMembers = async (
  params: GroupMembersParams
): Promise<GroupMembersResponse> => {
  const response = await apiClient.post<GroupMembersResponse>('/group/members', params);
  return response.data;
};

/**
 * 移除小组成员
 */
export const removeGroupStudent = async (
  params: RemoveGroupStudentParams
): Promise<GroupOperateResponse> => {
  const response = await apiClient.post<GroupOperateResponse>('/group/remove-student', params);
  return response.data;
};

/**
 * 设为组长
 */
export const setGroupLeader = async (
  params: SetGroupLeaderParams
): Promise<GroupOperateResponse> => {
  const response = await apiClient.post<GroupOperateResponse>('/group/set-leader', params);
  return response.data;
};

/**
 * 添加小组成员
 */
export const addGroupStudents = async (
  params: AddGroupStudentsParams
): Promise<GroupOperateResponse> => {
  const response = await apiClient.post<GroupOperateResponse>('/group/add-students', params);
  return response.data;
};